package com.magic.smartcity.presenters;

import android.util.Log;

import com.magic.smartcity.beans.BalanceBean;
import com.magic.smartcity.beans.FeedbackBean;
import com.magic.smartcity.beans.pojo.FeedbackBody;
import com.magic.smartcity.beans.pojo.LoginPojo;
import com.magic.smartcity.beans.RegisterBean;
import com.magic.smartcity.beans.RegisterBody;
import com.magic.smartcity.beans.TokenBean;
import com.magic.smartcity.beans.UserBean;
import com.magic.smartcity.beans.pojo.MsgCodePojo;
import com.magic.smartcity.beans.pojo.ResultData;
import com.magic.smartcity.beans.pojo.ResultList;
import com.magic.smartcity.beans.pojo.UpdatePasswordPojo;
import com.magic.smartcity.interfaces.IUserCallBack;
import com.magic.smartcity.interfaces.IUserModel;
import com.magic.smartcity.interfaces.IUserPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @description:
 * @create: Create by Magic on 2022/2/8.
 */
public class UserPresenter implements IUserPresenter<IUserCallBack> {
    private static final String TAG = UserPresenter.class.getSimpleName();

    @Inject
    IUserModel mIUserModel;

    private static List<IUserCallBack> mIUserCallBacks = new ArrayList<>();

    @Inject
    UserPresenter() {
    }

    @Override
    public void getNowUser() {
        mIUserModel.getNowUser();
    }

    @Override
    public void loginUser(String name, String password) {
        Log.d(TAG, "请求用户登录");
        mIUserModel.loginUser(new LoginPojo(name, password));
    }

    @Override
    public void registerUser(String username, String password, String phonenumber) {
        Log.d(TAG, "请求用户注册");
        mIUserModel.register(new RegisterBody(username, password, phonenumber, "0"));
    }

    @Override
    public void updateUser( String nickname, String sex, String phonenumber, String idcard, String email) {
        Log.d(TAG, "请求更新用户个人信息");
        UserBean.UserBeanDo userBeanDo = new UserBean.UserBeanDo(nickname, email, phonenumber, sex, null, idcard);
        mIUserModel.updateUser( userBeanDo);
    }

    @Override
    public void updatePassword( String oldPassword, String newPassword) {
        if ((oldPassword.length() >= 6 && oldPassword.length() <= 16) && (newPassword.length() >= 6 && newPassword.length() <= 16)) {
            Log.d(TAG, "请求更新用户密码");
            mIUserModel.updatePassword( new UpdatePasswordPojo(oldPassword, newPassword));
        } else {
            for (IUserCallBack mIUserCallBack : mIUserCallBacks) {
                mIUserCallBack.error("密码长度必须大于6,小于16");
            }
        }

    }

    @Override
    public void getBalanceList() {
        Log.d(TAG, "请求获取账户余额");
        mIUserModel.getBalanceList();
    }

    @Override
    public void rechargeBalance( int money) {
        Log.d(TAG, "请求获取账户余额充值,金额="+money);
        mIUserModel.balanceRecharge(money);
    }

    @Override
    public void getFeedbackList() {
        Log.d(TAG, "请求获取账户意见反馈列表");
        mIUserModel.getFeedbackList();
    }

    @Override
    public void getFeedbackById( int id) {
        Log.d(TAG, "请求获取账户意见反馈详情id=" + id);
        mIUserModel.getFeedbackById( id);
    }

    @Override
    public void feedbackSubmit( String title, String content) {
        Log.d(TAG, "请求提交反馈信息");
        mIUserModel.feedbackSubmit(new FeedbackBody(title,content));

    }


    ///////////// 以下为  注册与取消注册

    @Override
    public void registerCallBack(IUserCallBack callback) {
        if (!mIUserCallBacks.contains(callback)) {
            Log.d(TAG, "注册了:::::" + callback);
            mIUserCallBacks.add(callback);
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void unRegisterCallBack(IUserCallBack callback) {
        Log.d(TAG, "取消注册了:::::" + callback);
        mIUserCallBacks.remove(callback);
        EventBus.getDefault().unregister(this);
    }

    //数据回来后
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusMessage(UserEventBusMessage userEventBusMessage) {
        for (IUserCallBack callBack : mIUserCallBacks) {
            //请求获取当前登录的User信息
            if (userEventBusMessage.getUser() != null) {
                callBack.getUserLoaded(userEventBusMessage.getUser());
            }
            //请求登陆后返回的数据token
            if (userEventBusMessage.getTokenBean() != null) {
                if (userEventBusMessage.getTokenBean().getCode() == 200) {
                    callBack.loginOk("" + userEventBusMessage.getTokenBean().getToken());
                } else if (userEventBusMessage.getTokenBean().getCode() == 500) {
                    callBack.error("账号或密码错误!");
                    break;
                }
            }
            //请求注册后返回的数据
            if (userEventBusMessage.getRegisterBean() != null) {
                if (userEventBusMessage.getRegisterBean().getCode() == 200) {
                    callBack.registerOk("注册成功");
                    break;
                } else {
                    callBack.error(userEventBusMessage.getRegisterBean().getMsg());
                    break;
                }
            }
            //用户信息修改后返回的数据
            if (userEventBusMessage.getUpdateUser() != null) {
                if (userEventBusMessage.getUpdateUser().getCode() == 200) {
                    callBack.registerOk("用户个人信息修改成功");
                } else {
                    callBack.error("" + userEventBusMessage.getUpdateUser().getMsg());
                }
            }
            //用户密码修改后返回的数据
            if (userEventBusMessage.getPasswordUpdateResult() != null) {
                if (userEventBusMessage.getPasswordUpdateResult().getCode() == 200) {
                    callBack.updatePassword("密码修改成功");
                } else {
                    callBack.error(userEventBusMessage.getPasswordUpdateResult().getMsg());
                }
            }
            //用户意见反馈列表获取返回的数据
            if (userEventBusMessage.getFeedbackBeanResultList() != null) {
                if (userEventBusMessage.getFeedbackBeanResultList().getCode() == 200) {
                    callBack.feedbackListLoaded(userEventBusMessage.getFeedbackBeanResultList().getRows());
                } else {
                    callBack.error(userEventBusMessage.getFeedbackBeanResultList().getMsg());
                }
            }

            if (userEventBusMessage.getResultData()!=null){
                if (userEventBusMessage.getResultData().getCode()==200){
                    callBack.feedbackByIdLoaded(userEventBusMessage.getResultData().getData());
                }
            }
            if (userEventBusMessage.getSubmitFeedbackResult()!=null){
                if (userEventBusMessage.getSubmitFeedbackResult().getCode()==200){
                    callBack.feedbackSubmitOk("提交成功");
                }
            }
            if (userEventBusMessage.getBalanceResultList()!=null){
                if (userEventBusMessage.getBalanceResultList().getCode()==200){
                    callBack.balanceListLoaded(userEventBusMessage.getBalanceResultList().getRows());
                }
            }
            if (userEventBusMessage.getRechargeBalanceData()!=null){
                if (userEventBusMessage.getRechargeBalanceData().getCode()==200){
                    callBack.balanceRechargeOk(userEventBusMessage.getRechargeBalanceData().getMsg());
                }
            }
        }
    }

    public static class UserEventBusMessage {
        private UserBean.UserBeanDo user;
        private TokenBean tokenBean;
        private RegisterBean registerBean;
        private RegisterBean updateUser;
        private MsgCodePojo passwordUpdateResult;
        private ResultList<FeedbackBean> mFeedbackBeanResultList;
        private ResultData<FeedbackBean> mResultData;
        private ResultData submitFeedbackResult;
        private ResultList<BalanceBean> mBalanceResultList;
        private ResultData rechargeBalanceData;

        public ResultData getRechargeBalanceData() {
            return rechargeBalanceData;
        }

        public void setRechargeBalanceData(ResultData rechargeBalanceData) {
            this.rechargeBalanceData = rechargeBalanceData;
        }

        public ResultList<BalanceBean> getBalanceResultList() {
            return mBalanceResultList;
        }

        public void setBalanceResultList(ResultList<BalanceBean> balanceResultList) {
            mBalanceResultList = balanceResultList;
        }

        public ResultData getSubmitFeedbackResult() {
            return submitFeedbackResult;
        }

        public void setSubmitFeedbackResult(ResultData submitFeedbackResult) {
            this.submitFeedbackResult = submitFeedbackResult;
        }

        public ResultData<FeedbackBean> getResultData() {
            return mResultData;
        }

        public void setResultData(ResultData<FeedbackBean> resultData) {
            mResultData = resultData;
        }

        public ResultList<FeedbackBean> getFeedbackBeanResultList() {
            return mFeedbackBeanResultList;
        }

        public ResultList<FeedbackBean> setFeedbackBeanResultList(ResultList<FeedbackBean> feedbackBeanResultList) {
            return mFeedbackBeanResultList = feedbackBeanResultList;
        }

        public MsgCodePojo getPasswordUpdateResult() {
            return passwordUpdateResult;
        }

        public MsgCodePojo setPasswordUpdateResult(MsgCodePojo passwordUpdateResult) {
            this.passwordUpdateResult = passwordUpdateResult;
            return passwordUpdateResult;
        }

        public RegisterBean getUpdateUser() {
            return updateUser;
        }

        public RegisterBean setUpdateUser(RegisterBean updateUser) {
            this.updateUser = updateUser;
            return updateUser;
        }

        public RegisterBean getRegisterBean() {
            return registerBean;
        }

        public void setRegisterBean(RegisterBean registerBean) {
            this.registerBean = registerBean;
        }

        public TokenBean getTokenBean() {
            return tokenBean;
        }

        public void setTokenBean(TokenBean tokenBean) {
            this.tokenBean = tokenBean;
        }

        public UserBean.UserBeanDo getUser() {
            return user;
        }

        public void setUser(UserBean.UserBeanDo user) {
            this.user = user;
        }
    }
}
